package com.sr.service.impl;

import com.sr.dao.RolePermissionDao;
import com.sr.enums.ResponseCode;
import com.sr.exception.BusinessException;
import com.sr.ibase.impl.BaseServiceImpl;
import com.sr.pojo.SysRole;
import com.sr.pojo.SysRolePermission;
import com.sr.service.IRolePernissionService;
import com.sr.service.IRoleService;
import com.sr.util.R;
import com.sr.vo.req.RoleReqVo;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author xiaochi
 * @date 2022/5/3 18:23
 * @desc RoleServiceImpl
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__)
public class RoleServiceImpl extends BaseServiceImpl<SysRole> implements IRoleService {

    private final RolePermissionDao rolePermissionDao;
    private final IRolePernissionService rolePernissionService;

    /**
     * 新增角色
     * @param reqVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int createRole(RoleReqVo reqVo) {
        if (null == reqVo.getPermissionsIds() || reqVo.getPermissionsIds().isEmpty()){
            throw new BusinessException(ResponseCode.DATA_ERROR);
        }
        SysRole sysRole = reqVo.buildEntity();
        rolePernissionService.addRolePermission(reqVo.getPermissionsIds(),sysRole.getId());
        return this.insert(sysRole);
    }

    /**
     * 获取角色详情
     * @param roleId
     * @return
     */
    @Override
    public R<Set<String>> getRoleById(String roleId) {
        if (StringUtils.isBlank(roleId)){
            throw new BusinessException(ResponseCode.DATA_ERROR);
        }
        SysRole sysRole = this.findOne(roleId);
        if (sysRole == null){
            log.error("传入 的 id:{}不合法",roleId);
            throw new BusinessException(ResponseCode.DATA_ERROR);
        }
        // 通过角色id查询拥有的权限ids
        List<String> permissionIds = rolePermissionDao.getPermissionIdsByRoleId(roleId);
        return R.ok(new HashSet<>(permissionIds));
    }

    /**
     * 更新角色信息
     * @param reqVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRole(RoleReqVo reqVo) {
        SysRole sysRole = this.findOne(reqVo.getId());
        if (sysRole == null){
            throw new BusinessException(ResponseCode.DATA_ERROR);
        }
        SysRole role = reqVo.buildEntity();
        rolePernissionService.addRolePermission(reqVo.getPermissionsIds(),role.getId());
        return this.updateById(role);
    }

    /**
     * 删除角色信息
     * @param roleId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletedRole(String roleId) {
        SysRole sysRole = this.findOne(roleId);
        if (sysRole == null){
            throw new BusinessException(ResponseCode.DATA_ERROR);
        }
        SysRolePermission rolePermission = new SysRolePermission();
        rolePermission.setRoleId(roleId);
        rolePermissionDao.delete(rolePermission);
        sysRole.setDeleted(0);
        return this.updateById(sysRole);
    }
}
